$(function () {
    var transferClass_datagrid = $("#transferClass_datagrid");
    var transferClass_dialog = $("#transferClass_dialog");
    var transferClass_form = $("#transferClass_form");
    transferClass_datagrid.datagrid({
        url: '/transferClass/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {
                field: 'name', title: '学员姓名', width: 100,
            },
            {field: 'totalTuition', title: '总学费', width: 100,},
            {field: 'debt', title: '待缴学费', width: 100,},
            {field: 'prepaidTuition', title: '已交学费', width: 100,},
            {field: 'transferTime', title: '升班留级时间', width: 100,},
            {field: 'tel', title: '联系电话', width: 100,},
            {
                field: 'previousClass', title: '以前的班级', width: 100, formatter: function (value) {
                return value.className;
            }
            },
            {
                field: 'wantClass', title: '转入的班级', width: 100, formatter: function (value) {
                return value.className;
            }
            },
            {
                field: 'salemen', title: '营销人员', width: 100, formatter: function (value) {
                return value.username;
            }
            },
            {
                field: 'status', title: '审核状态', width: 100, formatter: function (value) {
                return value ? '<font color="green">已审核</font>' : '<font color="red">待审核</font>';
            }
            },
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#transferClass_btns",
        sortName: 'hireDate',
        sortOrder: 'desc'
    });
    transferClass_dialog.dialog({
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            transferClass_form.form('clear');
        }
    });
    var methodObj = {
        query:function () {
            var nowClass =$("[name=nowClass]").val();
            var beginDate =$("[name=beginDate]").val();
            var endDate = $("[name=endDate]").val();
            var keyword = $("[name=keyword]").val();
            transferClass_datagrid.datagrid('load',{
                nowClass:nowClass,
                beginDate:beginDate,
                endDate:endDate,
                keyword:keyword
            })
        },
        audit: function () {
            var row = transferClass_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '确定要审核该学员吗？', function (r) {
                if (r) {
                    $.get("/transferClass/audit.do", {transferClassId: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '审核成功', 'info')
                            transferClass_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        watch: function () {
            var row = transferClass_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $("#transferClass_form input").prop('readOnly', true);
            row['previousClass.id'] = row.previousClass.id;
            row['wantClass.id'] = row.wantClass.id;
            row['salemen.id'] = row.salemen.id;
            transferClass_form.form('load', row
            );
            transferClass_dialog.dialog('setTitle', '编辑升班留级');
            transferClass_dialog.dialog('open');
        },
        changeState: function () {
            var row = transferClass_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/transferClass/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            transferClass_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        del: function () {
            var row = transferClass_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要驳回该升班留级吗？', function (r) {
                if (r) {
                    $.get("/transferClass/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '驳回成功', 'info')
                            transferClass_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            transferClass_datagrid.datagrid('reload',[]);
        },

        save: function () {
            transferClass_form.form('submit', {
                url: '/transferClass/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObj.cancel();
                        transferClass_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            transferClass_dialog.dialog('close');
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObj[methodName]();
    })
})



